package gg.com.toolbox.sqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;

import gg.com.toolbox.bean.SecrecyBean;

/**
 * 数据库操作类
 * Created by gaoge on 2017/10/12.
 */

public class DBManager {
    //数据库对象
    private SQLiteDatabase db;
    // 账号表
    public static final String PASSWORD_TABLE = DatabaseHelper.PASSWORD_TABLE;

    public DBManager(Context context) {
        DatabaseHelper helper = new DatabaseHelper(context);
        // 因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0,
        // mFactory);
        // 所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
        db = helper.getWritableDatabase();
    }

    /**
     * 添加账号数据
     *
     * @param title    平台
     * @param username 账号
     * @param password 密码
     * @param remarks  备注
     */
    public void insertUserName(String title, String username, String password, String remarks) {
        String sql = "insert into " + PASSWORD_TABLE
                + "(title,username,password,remarks) values(?,?,?,?)";
        db.execSQL(
                sql,
                new Object[]{title, username, password, remarks});
    }

    /**
     * 数据修改
     *
     * @param title    平台
     * @param username 账号
     * @param password 密码
     * @param remarks  备注
     */
    public void updateUserName(String title, String username, String password, String remarks) {
        String sql = "update " + PASSWORD_TABLE
                + " set  username= ? , password= ? , remarks= ? where title = ? ";
        db.execSQL(
                sql,
                new Object[]{username, password, remarks, title});
    }

    /**
     * @return 查询到的数据
     */
    public ArrayList<SecrecyBean> selectUserName() {
        ArrayList<SecrecyBean> list = new ArrayList<>();
        SecrecyBean secrecyBean = null;
        //查询数据
        String sql = "select _id,title,username,password,remarks from "
                + PASSWORD_TABLE + " order by _id desc;";
        Cursor c = db.rawQuery(sql, null);
        while (c.moveToNext()) {
            secrecyBean = new SecrecyBean();
            secrecyBean.setId(c.getInt(c.getColumnIndex("_id")));
            secrecyBean.setTitle(c.getString(c.getColumnIndex("title")));
            secrecyBean.setUserName(c.getString(c.getColumnIndex("username")));
            secrecyBean.setPassWord(c.getString(c.getColumnIndex("password")));
            secrecyBean.setRemarks(c.getString(c.getColumnIndex("remarks")));
            list.add(secrecyBean);
        }
        return list;
    }

    /**
     * 删除数据
     *
     * @param _id 需要删除的数据id
     */
    public void deleteUserNameItem(int _id) {
        String sql = "delete from " + PASSWORD_TABLE + " where _id=" + _id;
        db.execSQL(sql);
    }

    /**
     * 清空表
     *
     * @param data 需要删除的表名
     */
    public void delete(String data) {
        String sql = "delete from " + data;
        db.execSQL(sql);
        //初始化表ID
        db.execSQL("update sqlite_sequence set seq=0 where name='" + data + "'");
    }
}
